from Base.HttpBase import HttpBase
from Base.ReadExcel import Read_Excel
import os
from Base import Log, GetYamlFile
import requests
log = Log.Log()

'''
获取登录的cookie
'''


def getCookie():
    # try:
        YmlList = GetYamlFile.getYamlFile()
        TestCasePath = YmlList["TestCasePath"]
        TestDomainName = YmlList["TestDomainName"]
        PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
        REPORT_XLSX_DIR = os.path.join(PROJECT_ROOT, TestCasePath)
        readBook = Read_Excel(REPORT_XLSX_DIR)
        DomainUrl = readBook.read_Excel(TestDomainName, 1, 0)
        GetCookieInterface = readBook.read_Excel(TestDomainName, 1, 1)
        url = DomainUrl + GetCookieInterface
        print(url)
        # data={'username':'admin','password':'password'}
        data = readBook.read_Excel(TestDomainName, 1, 2)
        # print(data)
        data = eval(data)
        # print(data)
        # print(type(data))
        headers = {
            "Content-Type": "application/json"
        }
        cookie = HttpBase(url, data, "Post", headers=headers).getHttpHeaders().get("access_token")
        # cookie = HttpBase(url, data, "Post", headers).getHttpBody()
        # body = requests.post(url, data, headers=headers)
        # body = requests.post(url, data, "Post", headers=headers)
        # print(str(body.content,'utf-8'))
        print(cookie)
        # log.info("得到cookie："+cookie)
        return cookie
    # except:
    #     log.error("获取cookies失败")


if __name__ == '__main__':
    print("test")
    x = getCookie()
